//
//  YDYTopView.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/9/20.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYTopView.h"
//#import "YDYSearchView.h"

#define btnW [UIScreen mainScreen].bounds.size.width / 5.0
@interface YDYTopView()
/** 按钮下方的白色小横线 */
@property (nonatomic, weak) UIView *lineView;
/** 记录当前按的是哪个按钮 */
@property (nonatomic, assign) NSInteger seletedBtnTag;
/** 头像 */
@property (nonatomic, weak) IBOutlet UIImageView *profileImageView;
@end

@implementation YDYTopView

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
       
    CGFloat btnH = 30;
    CGFloat btnY = 80;
    
    for (int i = 0; i < titleArray.count; i++) {
        UIButton * btn = [[UIButton alloc]init];
        btn.frame = CGRectMake( btnW * i, btnY, btnW, btnH);
        [self addSubview:btn];
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [btn setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    //为按钮下方添加一个滑动的小横线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(12, 111, btnW - 24, 3)];
    lineView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:1];
    lineView.layer.cornerRadius = 1.5;
    self.lineView = lineView;
    [self addSubview:lineView];
    
    //设置用户没点击的时候，默认选中第一个按钮
    
    UIButton *btn = [self viewWithTag:1];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font  = [UIFont systemFontOfSize:17];
    self.seletedBtnTag = 1;
    
    self.profileImageView.image = [self.profileImageView.image ydy_circleImage];
//    YDYLog(@"%f",self.profileImageView.layer.cornerRadius);
}

//搜索
-(IBAction)search:(id)sender{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:ydy_showSearchViewNotification object:nil];
//    YDYSearchView *searchV = [YDYSearchView searchView];
//    searchV.frame = CGRectMake(0, 0, YDYScreenSize.width, YDYScreenSize.height);
//    searchV.alpha = 0;
//    [self addSubview:searchV];
//    [self bringSubviewToFront:searchV];
//    [UIView animateWithDuration:0.25 animations:^{
//        searchV.alpha = 1;
//    
//    }];
    
    
    
}

//用户头像被点击
-(IBAction)profileClick:(id)sender{
    [[NSNotificationCenter defaultCenter]postNotificationName:ydy_showSliderViewNotification object:nil];
}

//标题栏的几个按钮被点击
-(void)titleClick:(UIButton *)btn{
    //还原上次点击的按钮
    UIButton *lastBtn = [self viewWithTag:self.seletedBtnTag];
    [lastBtn setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
    lastBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font  = [UIFont systemFontOfSize:17];
    self.seletedBtnTag = btn.tag;
    
    
    CGRect lineviewFrame =  self.lineView.frame;
    lineviewFrame.origin.x = (btn.tag - 1) * btnW + 12;
    [UIView animateWithDuration:0.15 animations:^{
        
        self.lineView.frame = lineviewFrame;
        [self layoutIfNeeded];
    }];
    
    if ([self.delegate respondsToSelector:@selector(titleBtndidClick:)]) {
        
        [self.delegate titleBtndidClick:btn];
    }

}


#pragma mark -
#pragma mark -setter
-(void)setSeletedTag:(NSInteger)seletedTag{
    _seletedTag = seletedTag + 1;
    UIButton *btn = [self viewWithTag:_seletedTag];
    [self titleClick:btn];
}
@end
